lane :app_auto do |options|
  workspace = options[:workspace]
  input_file = options[:input_file]
  ouput_file = options[:ouput_file]
  UI.important "workspace: #{workspace}"
  UI.important "input_file: #{input_file}"
  UI.important "ouput_file: #{ouput_file}"

  app_config = YAML.load_file(input_file)
  scheme = app_config['scheme']
  app_identifier = app_config['app_identifier']
  bundle_version = app_config['bundle_version']
  xcworkspace = File.expand_path(app_config['xcworkspace'], workspace)
  configuration = app_config['configuration']
  export_method = app_config['export_method']
  # pp app_config


  message = ["🎉 构建成功"]
  h = {}

  # 1.
  pod_install(workspace: workspace)

  # 2.
  ret = build(
    workspace: workspace,
    scheme: scheme,
    app_identifier: app_identifier,
    bundle_version: bundle_version,
    xcworkspace: xcworkspace,
    configuration: configuration,
    export_method: export_method
  )
  ipa_output_path = ret[:ipa_output_path]
  output_directory = ret[:output_directory]
  # ipa_output_path = '/Users/xiongzenghui/Desktop/App/derived_data/products/output.ipa'
  # output_directory = '/Users/xiongzenghui/Desktop/App/derived_data/products'
  h[:ipa_size] = file_size(ipa_output_path)
  message << ">ipa大小: #{h[:ipa_size]}"

  # 3.
  ipa_download_url = upload(file_path: ipa_output_path)
  # ipa_download_url = 'http://127.0.0.1:5000/download/output.ipa'
  h[:ipa_download_url] = ipa_download_url
  message << ">ipa 下载地址: #{ipa_download_url}"

  # 4.
  info_plist_path = File.expand_path("info.plist", output_directory)
  generate_plist(
    output: info_plist_path,
    app_identifier: app_identifier,
    bundle_version: bundle_version,
    ipa_download_url: ipa_download_url,
    title: "MR 测试包"
  )

  # 5.
  plist_download_url = upload(file_path: info_plist_path)
  # plist_download_url = 'http://127.0.0.1:5000/download/info.plist'
  h[:plist_download_url] = plist_download_url

  # 6.
  qr_code_url = qr_code(plist_download_url: plist_download_url)
  h[:qr_code_url] = qr_code_url
  message << ">安装二维码: #{qr_code_url}"

  # 7.
  text = message.join("\n")
  wechat(
    access_token_url: app_config['access_token_url'],
    secret: app_config['secret'],
    agentid: app_config['agentid'],
    touser: ['xiongzenghui'],
    msgtype: 'text',
    msgcontent: text,
    send_message_url: app_config['send_message_url']
  )

  File.open(ouput_file, 'w+') { |f| f.write(JSON.pretty_generate(h)) }
end